package com.liu.stockmarket

import com.liu.stockmarket.utils.YMDHMS
import org.junit.Test

import org.junit.Assert.*
import java.math.BigDecimal
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.temporal.ChronoField

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }

    @Test
    fun localData() {
        val toEpochSecond = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)
        println(toEpochSecond)
        println(System.currentTimeMillis())

        println(LocalDateTime.ofEpochSecond(toEpochSecond, 0, ZoneOffset.UTC).YMDHMS())
    }


    @Test
    fun a() {
        // 成本
        var m = BigDecimal.ZERO;
        // 数量
        var n = BigDecimal.ZERO

        listOf(BigDecimal("158.30"), BigDecimal("163.37"))

        var a = 0;

       getzhuanqianData() .forEach {
            if (it["type"] == "1") {
                val money = BigDecimal(it["money"]).setScale(5)
                val number = BigDecimal(it["nubmer"]).setScale(5)

                val totalBuyMoney = money * number


                m = (m*n + totalBuyMoney+BigDecimal("5").setScale(5)) / (n+number)
                n += number
            } else {
//                val shoupan = if (a == 0) {
//                    BigDecimal("158.30")
//                }else{
//                    BigDecimal("163.37")
//                }
//                // 红
//                if(BigDecimal(it["money"])>shoupan){
//                    m = (m*n- (BigDecimal(it["money"])-shoupan)*BigDecimal(it["nubmer"]))/n
//                    n -= BigDecimal(it["nubmer"])
//                }else{
//                    // 绿
//                    m = (m*n- (shoupan-BigDecimal(it["money"]))*BigDecimal(it["nubmer"]))/n
//                    n -= BigDecimal(it["nubmer"])
//                }
                val money = BigDecimal(it["money"]).setScale(5)
                val number = BigDecimal(it["nubmer"]).setScale(5)

                // 当时的总金额
                val totalMoney = n * money

                // 当时卖出的金额
                val sellMoney = money * number

                val youMoney = m * n

                n -= number

                m = (youMoney - sellMoney+BigDecimal("18")) / n


            }
        }
        println("单价：${m.setScale(2)}")
        println("数量：${n.setScale(2)}")
    }

    private fun getBoTeData()=mutableListOf(
        mapOf("type" to "1", "money" to "128", "nubmer" to "20"),
        mapOf("type" to "1", "money" to "172.18", "nubmer" to "10"),
        mapOf("type" to "1", "money" to "170", "nubmer" to "10"),
        mapOf("type" to "0", "money" to "158.82", "nubmer" to "10"),
        mapOf("type" to "0", "money" to "157.88", "nubmer" to "20"),
        mapOf("type" to "1", "money" to "157", "nubmer" to "10"),
    )

    private fun getTianHeData()=mutableListOf(
        mapOf("type" to "1", "money" to "140.73", "nubmer" to "10"),
        mapOf("type" to "1", "money" to "150.81", "nubmer" to "10"),
        mapOf("type" to "1", "money" to "152.10", "nubmer" to "10"),
        mapOf("type" to "0", "money" to "147.00", "nubmer" to "10"),
        mapOf("type" to "0", "money" to "148.00", "nubmer" to "10"),
        mapOf("type" to "1", "money" to "147.95", "nubmer" to "10"),
        mapOf("type" to "1", "money" to "146.87", "nubmer" to "10"),
    )

    private fun getzhuanqianData() = mutableListOf(
        mapOf("type" to "1", "money" to "10", "nubmer" to "200"),
        mapOf("type" to "0", "money" to "200", "nubmer" to "180"),
    )

}